home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / progut~1 / proff.zoo / look.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-07-04  |  3.6 KB  |  205 lines

  1. /*
  2.  * from K&R "The C Prograamming language"
  3.  * Table lookup routines
  4.  *
  5.  */
  6. #define INLOOK 1
  7.  
  8. #include <stdio.h>
  9. #include "lookup.h"
  10.  
  11. #ifndef __GNUC_TOS__
  12. #ifdef __GNUC_MINIX__
  13. #include <std.h>
  14. #else
  15. extern char *strcpy(), *strcat();
  16. #ifdef __GNUC__
  17. extern void *malloc();
  18. #else
  19. extern char *malloc();
  20. #endif
  21. extern unsigned int strlen();
  22. extern FILE *fopen() ;
  23. #endif
  24. typedef unsigned int size_t;
  25. #else
  26. #include <types.h>
  27. #include <stddef.h>
  28. #include <stdlib.h>
  29. #include <memory.h>
  30. #include <string.h>
  31. #include <unixlib.h>
  32. #include <time.h>
  33. #endif
  34.  
  35. extern struct
  36. lexlist    (*(*lextable))[];/* global pointer for lexical analyser hash table */
  37.  
  38. /*
  39.  * hash - for a hash value for string s
  40.  *
  41.  */
  42. int hash(s)
  43. char *s;
  44. {
  45.     int    hashval;
  46.  
  47.     for (hashval = 0; *s != '\0';)
  48.         hashval += *s++;
  49.     return (hashval % HASHMAX);
  50. }
  51.  
  52. /*
  53.  * lookup - lookup for a string s in the hash table
  54.  *
  55.  */
  56. struct hashlist
  57. *lookup(s, hashtab)
  58. char *s;
  59. struct hashlist *hashtab[];
  60. {
  61.     struct hashlist *np;
  62.  
  63.     for (np = hashtab[hash(s)]; np != NULL; np = np->next)
  64.         if (strcmp(s, np->name) == 0)
  65.             return(np);    /* found     */
  66.     return(NULL);        /* not found */
  67. }
  68.  
  69. /*
  70.  * install - install a string name in hashtable and its value def
  71.  * at a given hashtable.
  72.  */
  73. struct hashlist
  74. *install(name,def,hashtab)
  75. char *name;
  76. char *def;
  77. struct hashlist *hashtab[];
  78. {
  79.     int hashval;
  80.     struct hashlist *np, *lookup();
  81. #ifdef __GNUC__
  82.     char *strsave();
  83.     void *malloc(size_t);
  84. #else
  85.     char *strsave(), *malloc();
  86. #endif
  87.  
  88.     if ((np = lookup(name, hashtab)) == NULL) {    /* not found.. */
  89.         np = (struct hashlist *) malloc((size_t)sizeof(*np));
  90.                 if (np == NULL)
  91.             return(NULL);
  92.         if ((np->name = strsave(name)) == NULL)
  93.             return(NULL);
  94.         hashval = hash(np->name);
  95.         np->next = hashtab[hashval];
  96.         hashtab[hashval] = np;
  97.     } else                    /* found..     */
  98.         free(np->def);            /* free prev.  */
  99.     if ((np->def = strsave(def)) == NULL)
  100.         return(NULL);
  101.     return(np);
  102. }
  103.  
  104. /*
  105.  * strsave - save string s somewhere
  106.  *
  107.  */
  108. char
  109. *strsave(s)
  110. char *s;
  111. {
  112. #ifdef __GNUC__
  113.     char *p;
  114.     void *malloc(size_t);
  115. #else
  116.     char *p, *malloc();
  117. #endif
  118.     register int n;
  119.  
  120.     n = (int)strlen(s) + 1;
  121.     if ((p = malloc((size_t)n)) != NULL)
  122.             strcpy(p, s);
  123.     return(p);
  124. }
  125.  
  126. /*
  127.  * lexinstal - instal a string name in hashtable and its value
  128.  *           used by lexical analyser to quickly match a token
  129.  *           and return its lexical value.
  130.  *
  131.  */
  132. struct lexlist
  133. *lexinstal(name,val,flag,lextable)
  134. char *name;
  135. int val;
  136. int flag;
  137. struct lexlist *lextable[];
  138. {
  139.     int hashval;
  140.     struct lexlist *np,*lexlook();
  141. #ifdef __GNUC__
  142.     char *strsave();
  143.     void *malloc(size_t);
  144. #else
  145.     char *strsave(), *malloc();
  146. #endif
  147.  
  148.     if ((np = lexlook(name,lextable)) == NULL) {    /* not found.. */
  149.         np = (struct lexlist *) malloc((size_t)sizeof(*np));
  150.         if (np == NULL)
  151.             return(NULL);
  152.         if ((np->name = strsave(name)) == NULL)
  153.             return(NULL);
  154.         hashval = hash(np->name);
  155.         np->link = lextable[hashval];
  156.         lextable[hashval] = np;
  157.     }
  158.     np->val = val;                /* replace prev */
  159.     np->flag = flag;
  160.     return(np);
  161. }
  162.  
  163. /*
  164.  * lexlook - lookup for a string s in the hash table
  165.  *         used by lexinstal only.
  166.  *
  167.  */
  168. struct lexlist
  169. *lexlook(s,table)
  170. char *s;
  171. struct lexlist *table[];
  172. {
  173.     struct lexlist *np;
  174.  
  175.     for (np = table[hash(s)]; np != NULL; np = np->link)
  176.         if (strcmp(s, np->name) == 0)
  177.             return(np);    /* found     */
  178.     return(NULL);        /* not found */
  179. }
  180.  
  181. /*
  182.  * remove an item from the hash table forever
  183.  *
  184.  */
  185. struct lexlist
  186. *Remove(s, table)
  187. char *s;
  188. struct lexlist *table[];
  189. {
  190.     struct lexlist *np, *xp;
  191.  
  192.     np = table[hash(s)];
  193.     xp = np; 
  194.     while (np != NULL) {
  195.         if (strcmp(s, np->name) == 0) {
  196.             xp->link = np->link;    /* remove the link */
  197.             return(np);        /* return the lost */
  198.         }
  199.         xp = np; 
  200.         np = np->link;
  201.     }
  202.     return(NULL);
  203. }
  204.  
  205.